package com.itheima.leetcode;

/**
 * <h1>买卖股票的最佳时机含冷冻期</h1>
 * <h3>卖出股票后，你无法在第二天买入股票 (即冷冻期为 1 天)</h1>
 * <h3>注意：你不能同时参与多笔交易（你必须在再次购买前出售掉之前的股票）。</h1>
 */
public class SharesIIILeetcode309 {

    static int maxProfit(int[] prices) {
        if (1 == prices.length) {
            return 0;
        }
        int[] buy = new int[prices.length];
        int[] sell = new int[prices.length];
        buy[0] = -prices[0];
        sell[0] = 0;
        buy[1] = Math.max(-prices[0], -prices[1]);
        sell[1] = Math.max(sell[0], buy[0] + prices[1]);
        for (int i = 2; i < prices.length; i++) {
            buy[i] = Math.max(buy[i - 1], sell[i - 2] - prices[i]);
            sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);
        }
        return sell[prices.length - 1];
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{1, 2, 3, 0, 2})); // 3
    }
}
